package com.bdmc.report.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.bdmc.common.core.page.TableDataInfo;
import com.bdmc.common.core.domain.PageQuery;
import org.springframework.stereotype.Service;
import com.bdmc.common.core.mybatisplus.core.ServicePlusImpl;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.bdmc.common.utils.StringUtils;
import com.bdmc.report.domain.bo.ReportCheckWorkBo;
import com.bdmc.report.domain.vo.ReportCheckWorkVo;
import com.bdmc.report.domain.ReportCheckWork;
import com.bdmc.report.mapper.ReportCheckWorkMapper;
import com.bdmc.report.service.IReportCheckWorkService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 人员考勤Service业务层处理
 *
 * @author bdmc
 * @date 2022-03-11
 */
@Service
public class ReportCheckWorkServiceImpl extends ServicePlusImpl<ReportCheckWorkMapper, ReportCheckWork, ReportCheckWorkVo> implements IReportCheckWorkService {

    @Override
    public ReportCheckWorkVo queryById(Long checkWorkId){
        return getVoById(checkWorkId);
    }

    @Override
    public TableDataInfo<ReportCheckWorkVo> queryPageList(ReportCheckWorkBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<ReportCheckWork> lqw = buildQueryWrapper(bo);
        Page<ReportCheckWorkVo> result = pageVo(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    @Override
    public TableDataInfo<ReportCheckWorkVo> mySelectPage(ReportCheckWorkBo bo, PageQuery pageQuery) {
        return TableDataInfo.build(baseMapper.mySelectPage(pageQuery.build(),bo));
    }

    @Override
    public List<ReportCheckWorkVo> queryList(ReportCheckWorkBo bo) {
        return baseMapper.mySelectPage(bo);
    }

    private LambdaQueryWrapper<ReportCheckWork> buildQueryWrapper(ReportCheckWorkBo bo) {
        LambdaQueryWrapper<ReportCheckWork> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getEffectDate()), ReportCheckWork::getEffectDate, bo.getEffectDate());
        lqw.eq(StringUtils.isNotBlank(bo.getWorkPlanType()), ReportCheckWork::getWorkPlanType, bo.getWorkPlanType());
        lqw.eq(bo.getWorkState() != null, ReportCheckWork::getWorkState, bo.getWorkState());
        return lqw;
    }

    @Override
    public Boolean insertByBo(ReportCheckWorkBo bo) {
        ReportCheckWork add = BeanUtil.toBean(bo, ReportCheckWork.class);
        validEntityBeforeSave(add);
        boolean flag = save(add);
        if (flag) {
            bo.setCheckWorkId(add.getCheckWorkId());
        }
        return flag;
    }

    @Override
    public Boolean updateByBo(ReportCheckWorkBo bo) {
        ReportCheckWork update = BeanUtil.toBean(bo, ReportCheckWork.class);
        validEntityBeforeSave(update);
        return updateById(update);
    }

    /**
     * 保存前的数据校验
     *
     * @param entity 实体类数据
     */
    private void validEntityBeforeSave(ReportCheckWork entity){
        //TODO 做一些数据校验,如唯一约束
    }

    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return removeByIds(ids);
    }
}
